﻿
function NemoSorter(holder) {
    if (holder) {

        this.UI = new Object();
        this.UI.Holder = holder;

        this.SortRowsSelector = $(this.UI.Holder.attr('data-sortable'));
        this.SorterId = $(this.UI.Holder.attr('data-sortidselect')).val();

        this.SortOptions = new Array();

        var sorter = this;
        this.UI.Holder.find('*[data-type=sortoption]').each(function () {
            var nso = new NemoSorterOption(sorter, $(this));
            sorter.SortOptions.push(nso);
        });

        this.UI.Holder.data('sorter', this);

        if (this.SorterId) {
            var s = $.cookie('sort-' + this.SorterId);
            if (s) {
                s = s.split('.');
                if (s.length == 2) {
                    for (var xs = 0; xs < this.SortOptions.length; xs++) {
                        if (this.SortOptions[xs].SortKey == s[0]) {
                            this.ChangeSort(this.SortOptions[xs], s[1]);
                            break;
                        }
                    }
                }
            }
        }
    }
};

NemoSorter.prototype.Init = function () {
    $('.sort-options').each(function () {
        var ns = new NemoSorter($(this));
    });
};

NemoSorter.prototype.RaiseSortChange = function (sender) {

    var sortChange = 'asc';
    if (sender.SortState == 'asc') sortChange = 'desc';
    this.ChangeSort(sender, sortChange);

};
NemoSorter.prototype.ChangeSort = function (sorterOption, sortDirection) {
    for (var xs = 0; xs < this.SortOptions.length; xs++) {
        this.SortOptions[xs].ChangeSortState('none');
    }
    sorterOption.ChangeSortState(sortDirection);

    var sortOpt = new Object();
    sortOpt.order = sortDirection;
    if (sorterOption.SortByAttribute)
        sortOpt.attr = sorterOption.SortByAttribute;
    sortOpt.useVal = sorterOption.UseValue;
    this.SortRowsSelector.tsort(sorterOption.SortBy, sortOpt);

    if (this.SorterId)
        $.cookie('sort-' + this.SorterId, sorterOption.SortKey + '.' + sortDirection);

}
function NemoSorterOption(sorter, jqItem) {
    this.Sorter = sorter;
    this.UI = new Object();
    this.UI.Holder = jqItem;

    this.SortState = 'none';
    if (this.UI.Holder.hasClass('sort-asc')) this.SortState = 'asc';
    if (this.UI.Holder.hasClass('sort-desc')) this.SortState = 'desc';

    this.SortBy = this.UI.Holder.attr('data-sortby');
    this.SortByAttribute = this.UI.Holder.attr('data-sortattr');
    this.UseValue = this.UI.Holder.attr('data-sortuseval') == 'true';
    this.SortKey = this.UI.Holder.attr('data-sortkey');
    this.UI.Holder.data('sorter-item', this).css('cursor', 'pointer').click(function (e) {
        e.stopPropagation();
        e.preventDefault();
        var sortItem = $(this).data('sorter-item');
        sortItem.Sorter.RaiseSortChange(sortItem);
    });
};
NemoSorterOption.prototype.ChangeSortState = function (state) {
    if (this.SortState == state) return;
    this.UI.Holder.removeClass('sort-' + this.SortState);
    if (state != 'none') {
        this.UI.Holder.addClass('sort-' + state);
        this.SortState = state;
    }
};
var sorter = new NemoSorter();
window.Nemo.RegisterController(sorter);
window.Nemo.Sorter = sorter;